package com.hucheng.seckill.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hucheng.common.utils.CommonUtils;
import com.hucheng.seckill.entity.OrderInfoDO;
import com.hucheng.seckill.entity.OrderItemDO;
import com.hucheng.seckill.mapper.OrderInfoMapper;
import com.hucheng.seckill.mapper.OrderItemMapper;
import com.hucheng.seckill.service.OrderService;
import com.hucheng.seckill.vo.OrderInfoVO;
import com.hucheng.seckill.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-01
 * @Description: 类描述
 **/
@Component
@Service(interfaceClass = OrderService.class, retries = -1, timeout = 5000)
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    @Transactional
    public OrderInfoVO createOrder(Integer userId, SeckillGoodsVO seckillGoodsVO) {
        //创建总订单
        OrderInfoDO orderInfoDO = new OrderInfoDO();
        orderInfoDO.setOrderNumber(CommonUtils.getOrderIdByTime());
        orderInfoDO.setUserId(userId);
        orderInfoDO.setStatus(0);
        orderInfoDO.setPayAmount(new BigDecimal(seckillGoodsVO.getGoodsPrice()));
        orderInfoDO.setTotalAmount(seckillGoodsVO.getSeckillPrice());
        orderInfoDO.setAddressId(0);
        orderInfoDO.setRemark("");
        orderInfoDO.setIsDel(0);

        Integer id = orderInfoMapper.insert(orderInfoDO);

        //创建子订单
        OrderItemDO orderItemDO = new OrderItemDO();
        orderItemDO.setOrderId(orderInfoDO.getOrderId());
        orderItemDO.setOrderItemNumber(CommonUtils.getOrderIdByTime());
        orderItemDO.setStatus(0);
        orderItemDO.setFormatId(1);
        orderItemDO.setUserId(userId);
        orderItemDO.setGoodsName(seckillGoodsVO.getGoodsName());
        orderItemDO.setGoodsNumber(1);
        orderItemDO.setGoodsPrice(seckillGoodsVO.getSeckillPrice());
        orderItemDO.setGoodsAmount(seckillGoodsVO.getSeckillPrice());
        orderItemDO.setIsDel(0);

        orderItemMapper.insert(orderItemDO);

        OrderInfoVO orderVO = new OrderInfoVO();
        orderVO.setOrderId(orderInfoDO.getOrderId());
        return orderVO;
    }
}
